package com.huanbao.auth.component;

import com.huanbao.auth.user.domain.AuthUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2021/3/22 10:40
 * @description: TODO           jwt增强器，根据自己的业务放入增强字段
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
public class CustomTokenEnhancer implements TokenEnhancer {


    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

        AuthUser authUser = (AuthUser) authentication.getPrincipal();

        final Map<String, Object> additionalInfo = new HashMap<>();

        final Map<String, Object> retMap = new HashMap<>();

        // 将不敏感的信息放入 token 中，通过jwt传递
        additionalInfo.put("userId", authUser.getNwUserDetails().getSysUser().getId());
        additionalInfo.put("loginName", authUser.getNwUserDetails().getSysUser().getLoginname());
        additionalInfo.put("useremail", authUser.getNwUserDetails().getSysUser().getUseremail());
//        additionalInfo.put("menusList", authUser.getNwUserDetails().getSysMenus());               // 还是在掉接口吧，不然jwt太长
        // 在check_token 方法中 authorities 中会显示
//        additionalInfo.put("permissionList", authUser.getNwUserDetails().getSysPermissions());

        retMap.put("additionalInfo", additionalInfo);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(retMap);
        return accessToken;

    }


}
